//
//  LoginApIModel.m
//  MobileOffice
//
//  Created by jjkj on 17/3/15.
//  Copyright © 2017年 liaoningjujin. All rights reserved.
//

#import "LoginApIModel.h"
#import "AFNetworkReachabilityManager.h"
@implementation LoginApIModel

+ (void)updateJPUSHRegistrationID {
//    //如果用户登录
//    if ([DataCheck isValidString:[LZXHelper getSessionID]]) {
//        NSLog(@"registrationID---->%@",[JPUSHService registrationID]);
//        NSString *  registrationID = [[CommClass sharedCommon] objectForKey:@"registrationID"];;
//        if ([DataCheck isValidString:[JPUSHService registrationID]]) {
//            registrationID = [JPUSHService registrationID];
//            [[CommClass sharedCommon] setObject:registrationID forKey:@"registrationID"];
//        }
//        [ClientRequest SOAPMthod:updateUUID
//                        soapBody:@{@"uuid":registrationID?registrationID:@"",
//                                   @"type":@1 /** 1极光推送 2 百度推送*/
//                                   }
//                         success:^(id responseObject) {
//                             [LZXHelper hideLoadViewWithView:kWindow];
//                         } failure:^(NSError *error) {
//                             [LZXHelper hideLoadViewWithView:kWindow];
//                         }];
// 
//    }
//    
}

+ (void)updateApp:(UIViewController *)viewController
{
    [ClientRequest SOAPMthod:appUpdate
                    soapBody:@{@"deviceType":@"IOS",
                               @"AppVersion":APP_CLIENT_VERSION
                               }
                     success:^(id responseObject) {
                         
                         NSString *serverBuildVersion = [responseObject objectForKey:@"version"];
                         NSString *clientBuildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
                       
                         if ([serverBuildVersion integerValue] > [clientBuildVersion integerValue]) {
                             
                             NSString * toestString = [NSString stringWithFormat:@"您的设备处于%@联网模式。要更新吗？",[self getNetworkName]];
                           
                                 [LZXHelper postAlertViewControllerWithTitle:@"新版本更新" message:toestString cancelButtonTitle:@"取消" otherButtonTitle:@"确定" cancelEvent:^{
                                     
                                 } otherEvent:^{
                                     NSString * url = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",[responseObject objectForKey:@"ipaUrl"]];
                                     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
                                 } viewController:viewController];
                         }
                     
                     } failure:^(NSError *error) {
                }];
}

- (NSString *)getCurrentVersion {

    NSString *clientVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
   
    
    return clientVersion;
}

-(BOOL)needUpdate:(NSString *)serverVersion compareCurrentVsersion:(NSString *)currentVersion {

    NSArray *newVersionArray = [serverVersion componentsSeparatedByString:@"."];
    NSArray *oldVersionArray = [currentVersion componentsSeparatedByString:@"."];
    
    NSInteger count = oldVersionArray.count > newVersionArray.count ? newVersionArray.count:oldVersionArray.count;
    
    for (NSInteger i = 0; i < count;i++) {
        
        NSString *newVersionStr = newVersionArray[i];
        NSString *oldVersionStr = oldVersionArray[i];
        
        if ([newVersionStr integerValue] > [oldVersionStr integerValue]) {
            return YES;
        }else if ([newVersionStr integerValue] >= [oldVersionStr integerValue]){
        
            return NO;
        }
    }
    
    if (oldVersionArray.count < newVersionArray.count) {
        return YES;
    }
    
    return NO;

}
/** * *
 * 登录
 * postParams 登录信息
 */
+ (void)login:(NSDictionary *)postParams success:(ResponseBlock)response
{
    [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
    [ClientRequest SOAPMthod:Login
                    soapBody:postParams
                     success:^(id responseObject) {
                          [MBProgressHUD hideLoadViewWithView:kWindow];
                         if ([DataCheck isValidDictionary:responseObject]) {
                             if (response) {
                                 response(responseObject);
                             }
                         }
                     } failure:^(NSError *error) {
                         [MBProgressHUD postMessageWithText:@"请求失败" View:kWindow];
                         
                         NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
                         NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                         NSLog(@"服务器的错误原因：%@",str);
                     }];
}

//检测网络状况
+ (NSString *)getNetworkName
{
    __block NSString * isExistenceNetwork = @"";
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                //未知网
                isExistenceNetwork = @"";
                
                break;
            case AFNetworkReachabilityStatusNotReachable:
                //无网
                isExistenceNetwork = @"";
                
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                //蜂窝数据网, 无线网
                isExistenceNetwork = @"3G/4G";
                
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                //wifi网
                isExistenceNetwork = @"wifi";
                break;
            default:
                break;
        }
        
    }];
    return isExistenceNetwork;
}

@end
